﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Collections;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using Models;

namespace Web
{
    public partial class workreading : System.Web.UI.Page
    {
        private int id = 0;
        private int wid = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            
            if (!IsPostBack)
            {
                UpdateParamInfoLogic.UpdateAllWorksAbout();
                try
                {
                    id = Int32.Parse(Request.QueryString["id"].ToString());
                }
                catch
                {
                    id = 0;
                    wid = 0;
                }
                try
                {
                    wid = Int32.Parse(Session["WCategoryID"].ToString());
                }
                catch
                {
                    wid = Int32.Parse(Request.QueryString["wid"].ToString().Trim());
                }
                ArrayList prelist = WorksInfoLogic.GetPreWorksInfo(id,wid);
                ArrayList nextlist = WorksInfoLogic.GetNextWorksInfo(id,wid);

                if ((Int32.Parse(prelist[2].ToString().Trim()) > 0) && (Int32.Parse(nextlist[2].ToString().Trim()) > 0))
                {
                    btn_pre.ImageUrl = "~/images/wnp.png";
                    btn_next.ImageUrl = "~/images/wnn.png";
                    btn_pre.Enabled = true;
                    btn_next.Enabled = true;
                }
                else if ((Int32.Parse(prelist[2].ToString().Trim()) > 0) && (Int32.Parse(nextlist[2].ToString().Trim()) <= 0))
                {
                    btn_pre.ImageUrl = "~/images/wnp.png";
                    btn_next.ImageUrl = "~/images/bnn.png";
                    btn_pre.Enabled = true;
                    btn_next.Enabled = false;
                }
                else if ((Int32.Parse(prelist[2].ToString().Trim()) <= 0) && (Int32.Parse(nextlist[2].ToString().Trim()) > 0))
                {
                    btn_pre.ImageUrl = "~/images/bnp.png";
                    btn_next.ImageUrl = "~/images/wnn.png";
                    btn_pre.Enabled = false;
                    btn_next.Enabled = true;
                }
                else
                {
                    btn_pre.ImageUrl = "~/images/bnp.png";
                    btn_next.ImageUrl = "~/images/bnn.png";
                    btn_pre.Enabled = false;
                    btn_next.Enabled = false;
                }
                
                WorksCategoryBindData();
                WorksInfoBindData(id);

                string countScript = " $($.post('counter.aspx', {wid:'" + id + "'},function(data){}));";
                ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "WorksCount", countScript, true);
            }
        }

        public void WorksCategoryBindData()
        {
            List<WorksCategoryInfo> categorylist = WorksCategoryInfoLogic.GetAllWorksCategoryInfo(wid);

            if (categorylist == null)
            {
                categorylist = new List<WorksCategoryInfo>();
                categorylist.Add(new WorksCategoryInfo());
            }

            WorkCategoryRepeater.DataSource = categorylist;
            WorkCategoryRepeater.DataBind();
        }

        public void WorksInfoBindData(int id)
        {
            List<WorksInfo> workslist = WorksInfoLogic.GetOneWorksInfo(id);
            if (workslist == null)
            {
                workslist = new List<WorksInfo>();
                workslist.Add(new WorksInfo());
            }

            WorksInfoData.DataSource = workslist;
            WorksInfoData.DataBind();

            /*绑定标题*/
            WorksInfo works = workslist.FirstOrDefault();
            Page.Title = works.Title;
        }


        public void WorksPicInfoBindData(DataList dl,int id)
        {
            List<WorksPicInfo> workpiclist = WorksPicInfoLogic.GetWorkAllPicInfo(id);
            if (workpiclist == null)
            {
                workpiclist = new List<WorksPicInfo>();
                workpiclist.Add(new WorksPicInfo());
            }
            dl.DataSource = workpiclist;
            dl.DataBind();
        }

        protected void WorksInfoData_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            int id = Int32.Parse(Request.QueryString["id"].ToString());
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) 
            {
                DataList dl = null;
                dl = (DataList)e.Item.FindControl("WorksPicData");
                WorksPicInfoBindData(dl,id);
            }
        }

        protected void btn_pre_Click(object sender, ImageClickEventArgs e)
        {
            id = Int32.Parse(Request.QueryString["id"].ToString().Trim());
            try
            {
                wid = Int32.Parse(Session["WCategoryID"].ToString());
            }
            catch
            {
                wid = Int32.Parse(Request.QueryString["wid"].ToString().Trim());
            }
            ArrayList prelist = WorksInfoLogic.GetPreWorksInfo(id,wid);
            int preWorksID = Int32.Parse(prelist[0].ToString().Trim());
            int preWokrsCategoryID = Int32.Parse(prelist[1].ToString().Trim());
            if (preWorksID != 0)
            {
                Response.Redirect("workreading.aspx?wid=" + preWokrsCategoryID + "&id=" + preWorksID);
            }
        }

        protected void btn_next_Click(object sender, ImageClickEventArgs e)
        {
            id = Int32.Parse(Request.QueryString["id"].ToString().Trim());
            try
            {
                wid = Int32.Parse(Session["WCategoryID"].ToString());
            }
            catch
            {
                wid = Int32.Parse(Request.QueryString["wid"].ToString().Trim());
            }
            ArrayList nextlist = WorksInfoLogic.GetNextWorksInfo(id,wid);
            int nextWorksID = Int32.Parse(nextlist[0].ToString().Trim());
            int nextWokrsCategoryID = Int32.Parse(nextlist[1].ToString().Trim());
            if (nextWorksID != 0)
            {
                Response.Redirect("workreading.aspx?wid=" + nextWokrsCategoryID + "&id=" + nextWorksID);
            }
        }
    }
}
